import {
	netConfig
} from "../config/net.config";

export const imgUrl = (e) => {
	if (!e) {
		return "";
	} else if (e.substring(0, 4) != "http") {
		return netConfig.cdnUrl + `${e}`;
	} else {
		return e;
	}
};

// 项目仅微信小程序
function requestPayment(data) {
	return new Promise((resolve, reject) => {
		uni.getProvider({
			service: 'payment',
			success: (res) => {
				if (!res.provider.includes('wxpay')) {
					uni.showToast({
						title: '微信授权失败',
						icon: 'error'
					});
					reject('无法授权微信支付');
					return;
				}

				uni.requestPayment({
					provider: 'wxpay',
					orderInfo: {

					},
					success: (res) => {
						resolve(res);
					},
					fail: (error) => {
						uni.showToast({
							title: '支付失败',
							icon: 'error'
						});
						reject(error);
					}
				});
			},
			fail: (error) => {
				reject(error);
				uni.showToast({
					title: '微信授权失败',
					icon: 'error'
				});
			}
		});
	});
}
// 微信小程序支付
export const miniPay = (params) => {
	return new Promise((resolve, reject) => {
		uni.requestPayment({
			provider: "wxpay",
			...params,
			success() {
				// uni.requestSubscribeMessage({
				// 	tmplIds: ['F_XBaU8dbLvxKF9reiOkPAObplbhhF14xbX4jmKRJFM']
				// })
				resolve();
			},
			fail() {
				reject({
					message: "已取消支付"
				});
			},
		});
	});
}